import { gameConfigHandle } from "core/config/gameConfig"
import { PlayerModel } from "core/model/playerModel"
import utils from "./utils"
import bagHandle from "core/handle/bagHandle"
import adventureHandle from "core/handle/adventureHandle"

class GameUtils {
    formatDateTime(time: number) {
        console.log("time", time)
        let date = new Date(time)
        // 构造 YYYY-MM-DD HH:mm:ss 格式
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0'); // 月份从0开始
        const day = String(date.getDate()).padStart(2, '0');
        const hours = String(date.getHours()).padStart(2, '0');
        const minutes = String(date.getMinutes()).padStart(2, '0');
        const seconds = String(date.getSeconds()).padStart(2, '0');
        return `${year}-${month}-${day}`;
    }
    getResourceAmount = (type, lvl) => {
        let adventureConfig = gameConfigHandle.gameConfig.adventure
        if (type == 'exp') {
            let baseValue = Math.floor(utils.getIncValues(adventureConfig.getResourceByFloor.exp, lvl))
            return baseValue
        } else if (type == 'gold') {
            let baseValue = Math.floor(utils.getIncValues(adventureConfig.getResourceByFloor.gold, lvl))
            return baseValue
        } else if (typeof type == 'number') {
            let baseValue = utils.getIncValues(adventureConfig.getItem, lvl)
            return baseValue
        }

    }
    getNeedResource(player: PlayerModel, needConfig, lvl, amount: number = 1, toLvl: number = 1) {
        let needs = needConfig.resources
        let needItems = {}
        let needNodes = []
        let totalNeeds = {}
        let err = 0
        if (needs) {
            for (let aLvl = lvl; aLvl < lvl + toLvl; aLvl++) {
                for (let i = 0; i < needs.length; i++) {
                    let need = needs[i]
                    if (need.limitMinLvl && aLvl < need.limitMinLvl) {
                        continue
                    }
                    if (need.limitMaxLvl && aLvl > need.limitMaxLvl) {
                        continue
                    }
                    let needType = need.name
                    let needValues: any = need.incValues
                    let lvlCoe = need.lvlCoe || 1
                    let valueCoe = need.valueCoe || 1
                    let addLvl = need.addLvl || 0
                    let _lvl = aLvl
                    if (need.maxLvl && _lvl > need.maxLvl) {
                        _lvl = need.maxLvl
                    }
                    _lvl = _lvl * lvlCoe + addLvl
                    let needAmount = utils.getIncValues(needValues, _lvl) * this.getResourceAmount(needType, _lvl) * amount * valueCoe
                    if (typeof needType == 'string') {
                        if (!needItems[needType]) {
                            needItems[needType] = 0
                        }
                        needItems[needType] += needAmount
                        // if (player.itemInfo[needType] < needAmount) {
                        //     err = 1
                        // }
                    } else if (typeof needType == 'number') {
                        if (!needItems[needType]) {
                            needItems[needType] = 0
                        }
                        needItems[needType] += needAmount
                        // if (bagHandle.getTotalNumByItemId(player.bag, needType) < needAmount) {
                        //     err = 1
                        // }
                    }
                    needNodes.push([needType, needAmount])
                }
            }
            utils.entries(needItems).forEach(([needType, needAmount]: [any, number]) => {
                if (isNaN(needType)) {
                    if (player.itemInfo[needType] < needAmount) {
                        err = 1
                    }
                } else {
                    needType = Number(needType)
                    if (bagHandle.getTotalNumByItemId(player.bag, needType) < needAmount) {
                        err = 1
                    }
                }
            })
        }

        needs = needConfig.items
        if (needs) {
            for (let aLvl = lvl; aLvl < lvl + toLvl; aLvl++) {
                for (let i = 0; i < needs.length; i++) {
                    let need = needs[i]
                    if (need.limitMinLvl && aLvl < need.limitMinLvl) {
                        continue
                    }
                    if (need.limitMaxLvl && aLvl > need.limitMaxLvl) {
                        continue
                    }
                    let needType = need.name
                    let needValues: any = need.incValues
                    let lvlCoe = need.lvlCoe || 1
                    let valueCoe = need.valueCoe || 1
                    let addLvl = need.addLvl || 0
                    let _lvl = aLvl
                    if (need.maxLvl && _lvl > need.maxLvl) {
                        _lvl = need.maxLvl
                    }
                    _lvl = _lvl * lvlCoe + addLvl
                    let needAmount = utils.getIncValues(needValues, _lvl) * amount * valueCoe
                    if (typeof needType == 'string') {
                        if (!needItems[needType]) {
                            needItems[needType] = 0
                        }
                        needItems[needType] += needAmount
                    } else if (typeof needType == 'number') {
                        if (!needItems[needType]) {
                            needItems[needType] = 0
                        }
                        needItems[needType] += needAmount
                    }
                    needNodes.push([needType, needAmount])
                }
            }
            utils.entries(needItems).forEach(([needType, needAmount]: [any, number]) => {
                if (isNaN(needType)) {
                    if (player.itemInfo[needType] < needAmount) {
                        err = 1
                    }
                } else {
                    needType = Number(needType)
                    if (bagHandle.getTotalNumByItemId(player.bag, needType) < needAmount) {
                        err = 1
                    }
                }
            })

        }
        return { err: err, needItems, needNodes }
    }
    useNeedResource(player: PlayerModel, needConfig, lvl, amount: number = 1) {
        let res = this.getNeedResource(player, needConfig, lvl, amount)
        if (res.err !== 0) {
            return res.needItems
        }
        res.needNodes.forEach(item => {
            let needType = item[0]
            let needAmount = item[1]
            if (typeof needType == 'string') {
                player.itemInfo[needType] -= needAmount
                if (!player.useInfo[needType]) {
                    player.useInfo[needType] = 0
                }
                player.useInfo[needType] += needAmount
            } else if (typeof needType == 'number') {
                bagHandle.removeItemByItemId(player.bag, needType, needAmount)
                if (!player.useInfo[needType]) {
                    player.useInfo[needType] = 0
                }
                player.useInfo[needType] += needAmount
            }
        })

        return 0
    }
    getMapMax(mapProgressMax: number) {
        return Math.floor(mapProgressMax / 100000000)
    }

    setMapMax(player: PlayerModel, progressMax: number, time: number) {
        let oriMapProgressMax = player.baseInfo.mapProgressMax
        let _progressMax = this.getMapMax(oriMapProgressMax)
        //2524608000000 - 2050year
        if (progressMax > _progressMax) {
            let newMapProgressMax = progressMax * 100000000 + Math.floor((2524608000000 - time) / 60000)
            player.baseInfo.mapProgressMax = newMapProgressMax
            return newMapProgressMax
        }
    }
    isPower(player: PlayerModel, powerConfig: any) {
        if (powerConfig.progress) {
            let { total, maxTotal } = adventureHandle.getAdventureMapProgress(player)
            if (maxTotal >= powerConfig.progress) {
                return true
            } else {
                return false
            }
        } else if (powerConfig.mapID) {
            if (!this.isMapLimit(player, powerConfig.mapID)) {
                return true
            } else {
                return false
            }
        }

    }
    isShow(player: PlayerModel, type: string) {
        let power = gameConfigHandle.gameConfig.power[type]
        if (power) {
            if (power.show !== null && power.show !== undefined) {
                return this.isPower(player, power.show)
            } else {
                return false
            }

        }
        return true
    }
    isUnlock(player: PlayerModel, type: string) {
        let power = gameConfigHandle.gameConfig.power[type]
        if (power) {
            return this.isPower(player, power.unlock)
        }
        return true
    }
    isMapLimit(player: PlayerModel, mapID: number) {
        let mapConfig = gameConfigHandle.configByID[mapID]
        let floor = player.adventure.mapProgress[mapID] || 0
        let newFloor = 0
        if (mapConfig.nextMapID) {
            newFloor = player.adventure.mapProgress[mapConfig.nextMapID] || 0
        }
        if (floor > mapConfig.maxFloor || newFloor > 0) {
            return false
        } else {
            return true
        }
    }
    getPowerLimit(player: PlayerModel, type: string) {
        let power = gameConfigHandle.gameConfig.power[type]
        return power
    }
}


let gameUtils = new GameUtils()
export default gameUtils